#include "Game.hpp"
#include "rgui/rgui.hpp"
#include "Button.hpp"

Game::Game() : 
	mWindow(new phx::RenderWindow(1280, 768, "Phoenix!")),
	mCamera(new phx::Camera(phx::vec3f(40.0f, 0.0f, 40.0f), phx::vec3f(0.0f, 0.0f, 0.0f))),
	mModel(new phx::Model()),
	mRenderTexture(new phx::RenderTexture(1280, 768)),
	//mRenderTexture(nullptr),
	mPostProcessing(false)
{
	mCamera->setDistance(3.0f);
	mModel->loadFromFile("MM\\MM.3DS");
	//mModel->setScale(phx::vec3f(0.1f, 0.1f, 0.1f));
	mModel->rotate(-90.0f, phx::vec3f(1.0f, 0.0f, 0.0f));
	mModel->setPosition(phx::vec3f(0.0f, -0.8f, 0.0f));
	mModel->setCamera(*mCamera);

	//std::vector<phx::Vertex> vertices;
	//phx::FBXLoader fbxLoader;
	//fbxLoader.loadFromFile("Link/Link.3ds", vertices);
}

void Game::enterGameLoop()
{
	mWindow->setKeyCallbackFunc(
		[this](phx::RenderWindow &window, int key, int scancode, int action, int mods)
	{
		if(action == GLFW_PRESS)
		{
			if(key == GLFW_KEY_P)
				mPostProcessing = !mPostProcessing;
			if(key == GLFW_KEY_ESCAPE)
				phx::leaveMainLoop();
		}
	});

	phx::Cursor::setPosition(phx::vec2i(mWindow->getSize().x * 0.5f, mWindow->getSize().y * 0.5f), *mWindow);

	ygg::Button button(phx::vec2f(0.0f, 0.0f), phx::vec2f(50, 15), phx::Colorf(0.0f, 0.0f, 0.0f, 0.8f));
	mWindow->setCursorVisible(false);

	mWindow->setMouseButtonCallbackFunc(
		[this](phx::RenderWindow &window, ygg::Mouse::Button button, ygg::Mouse::ButtonState buttonState)
	{
		switch(button)
		{
			case ygg::Mouse::LEFT:
			{
				phx::vec2i cursorPos = phx::Cursor::getPosition(window);
				if(buttonState == ygg::Mouse::ButtonState::PRESS)
					rgui::System::onMousePressed(rgui::vec2i(cursorPos.x, cursorPos.y));
				else if(buttonState == ygg::Mouse::ButtonState::RELEASE)
					rgui::System::onMouseReleased(rgui::vec2i(cursorPos.x, cursorPos.y));
				break;
			}
			case ygg::Mouse::SCROLL_UP:
				mCamera->setDistance(mCamera->getDistance() - 0.3f);
				break;
			case ygg::Mouse::SCROLL_DOWN:
				mCamera->setDistance(mCamera->getDistance() + 0.3f);
				break;
		}
	});

	mWindow->setRenderCallbackFunc(
		[this](phx::RenderWindow &window, unsigned int deltaTime)
	{
		phx::vec2i cursorPos = phx::Cursor::getPosition(window);
		cursorPos.x = cursorPos.x - window.getSize().width * 0.5f;
		cursorPos.y = cursorPos.y - window.getSize().height * 0.5f;
		mCamera->rotateAroundTarget(phx::vec3f(0.0f, int(cursorPos.y * 0.5f), -int(-cursorPos.x * 0.5f)));
		//mCamera->update();
		phx::Cursor::setPosition(window.getSize().width / 2.0f, window.getSize().height / 2.0f, window);

		window.pollEvent();
		window.clear(phx::Colorf(0.5f, 0.5f, 1.0f, 1.0f));
		//rgui::System::update(rgui::vec2i(cursorPos.x, cursorPos.y));

		if(mPostProcessing)
		{
			mRenderTexture->clear();
			phx::RenderTexture::use(*mRenderTexture);
			mModel->render();
			mRenderTexture->render();
		}
		else
			mModel->render();

		// Draw 2D stuff last (After 3D and render textures, otherwise 2D stuff wont be on top; Draw 2D stuff before 3D stuff if the 2D stuff should be in 3D? (Billboard))
		//rgui::System::render();

		window.display();
	});

	phx::mainLoop();
}

Game::~Game()
{
	delete mWindow;
	delete mCamera;
	delete mModel;
	delete mRenderTexture;
}